﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Sinacor.Infra.Common.ErrorLogging;
using System.Xml;

namespace Sinacor.Infra.Core.ProcessManager.Server.ErrorLogging
{
    public class ProcessEnvironmentData : LoginEnvironmentData
    {
        [XmlAttribute]
        public int? ProcessId { get; set; }
        [XmlAttribute]
        public string ProcessName { get; set; }

        protected override string ToStringOverride()
        {
            StringBuilder logData = new StringBuilder(base.ToStringOverride());

            if (!string.IsNullOrEmpty(logData.ToString()) &&
                (this.ProcessId != null || !string.IsNullOrEmpty(this.ProcessName)))
            {
                logData.AppendLine();
            }

            if (this.ProcessId != null)
            {
                logData.AppendFormat("ProcessId:\t{0}", this.ProcessId);
                logData.AppendLine();
            }

            if (!string.IsNullOrEmpty(this.ProcessName))
            {
                logData.AppendFormat("ProcessName:\t{0}", this.ProcessName);
                logData.AppendLine();
            }

            if (this.DbEnvironment != null)
            {
                logData.Append(this.DbEnvironment.ToString());
            }

            return logData.ToString();
        }

        public override void WriteTo(XmlWriter writer)
        {
            base.WriteTo(writer);

            if (this.ProcessId != null)
                writer.WriteElementString("ProcessId", this.ProcessId.ToString());

            if (!string.IsNullOrEmpty(this.ProcessName))
                writer.WriteElementString("ProcessName", this.ProcessName);

            if (this.DbEnvironment != null)
            {
                this.DbEnvironment.WriteTo(writer);
            }
        }
    }
}
